<template>
<transition name="list-fade">
  <div class="playlist" v-show="showFlag" @click="hide">
    <div class="list-wrapper" @click.stop>
      <div class="list-header">
        <h1 class="title">
          <i class="icon" :class="iconMode" @click.stop="changeMode"></i>
          <span class="text">{{modeText}}</span>
          <span class="clear" @click="showConfirm"><i class="icon-clear"></i></span>
        </h1>
      </div>
      <scroll class="list-content" :data="sequenceList" ref="listContent">
        <transition-group name="list" tag="ul">
          <li class="item" v-for="(item, index) of sequenceList" :key="item.id" @click="selecteItem(item, index)" ref="list">
            <i class="current" :class="currentIcon(item)"></i>
            <span class="text">{{item.name}}</span>
            <span class="like" @click.stop="toggleFavorite(item)"><i :class="getFavoriteIcon(item)"></i></span>
            <span class="delete"><i class="icon-delete" @click.stop="deleteOne(item)"></i></span>
          </li>
        </transition-group>
      </scroll>
      <div class="list-operate">
        <div class="add">
          <i class="icon-add"></i>
          <span class="text" @click="showAddSong">添加歌曲到队列</span>
        </div>
      </div>
      <div class="list-close" @click="closePlaylist">
        <span>关闭</span>
      </div>
    </div>
    <confirm ref="confirm" text="是否清空播放列表" @confirm="clear"></confirm>
    <add-song ref="addSong"></add-song>
  </div>
</transition>
</template>
<script>
import {mapActions} from 'vuex'
import Scroll from 'base/scroll/scroll'
import {playMode} from 'common/js/config'
import Confirm from 'base/confirm/confirm'
import {playerMixin} from 'common/js/mixin'
import AddSong from 'components/add-song/add-song'
export default {
  name: 'Playlist',
  mixins: [playerMixin],
  data () {
    return {
      showFlag: false
    }
  },
  computed: {
    modeText () {
      return this.mode === playMode.sequence ? '顺序播放' : this.mode === playMode.random ? '随机播放' : '单曲循环'
    }
  },
  // computed: {
  //   ...mapGetters(['sequenceList', 'currentSong', 'playList', 'mode'])
  // },
  methods: {
    showAddSong () {
      this.$refs.addSong.show()
    },
    clear () {
      this.hide()
      this.clearPlaylist()
    },
    showConfirm () {
      this.$refs.confirm.show()
    },
    deleteOne (item) {
      this.deleteSong(item)
    },
    scrollToCurrent (current) {
      this.$refs.listContent.refresh()
      let index = this.sequenceList.findIndex((song) => {
        return song.id === current.id
      })
      this.$refs.listContent.scrollToElement(this.$refs.list[index], 300)
    },
    selecteItem (item, index) {
      if (this.mode === playMode.random) {
        index = this.playList.findIndex((song) => {
          return song.id === item.id
        })
      }
      this.setCurrentIndex(index)
      this.setPlayState(true)
    },
    // ...mapMutations({
    //   setCurrentIndex: 'SET_CURRENT_INDEX',
    //   setPlayingState: 'SET_PLAYING_STATE'
    // }),
    ...mapActions(['deleteSong', 'clearPlaylist']),
    currentIcon (item) {
      if (item.id === this.currentSong.id) {
        return 'icon-play'
      } else {
        return ''
      }
    },
    closePlaylist () {
      this.$emit('close')
    },
    show () {
      this.showFlag = true
      setTimeout(() => {
        this.$refs.listContent.refresh()
        this.scrollToCurrent(this.currentSong)
      }, 20)
    },
    hide () {
      this.showFlag = false
    }
  },
  watch: {
    currentSong (newSong, oldSong) {
      if (newSong.id === oldSong.id || !this.showFlag) return
      this.scrollToCurrent(newSong)
    }
  },
  components: {
    Scroll,
    Confirm,
    AddSong
  }
}
</script>
<style lang="scss" scoped>
@import 'common/scss/variable.scss';
@import 'common/scss/mixin.scss';
.playlist{
  position: fixed;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  z-index: 200;
  background-color: $color-background-d;
  &.list-fade-enter-active, &.list-fade-leave-active{
    transition: opacity 0.3s;
    .list-wrapper{
      transition: all 0.3s;
    }
  }
  &.list-fade-enter, &.list-fade-leave-to{
    opacity: 0;
    .list-wrapper{
      transform: translate3d(0, 100%, 0);
    }
  }
  .list-wrapper{
    position: absolute;
    left: 0;
    bottom: 0;
    width: 100%;
    background-color: $color-highlight-background;
    .list-header{
      padding: 0.4rem 0.6rem 0.2rem 0.4rem;
      .title{
        display: flex;
        align-items: center;
        .icon{
          margin-right: 0.2rem;
          font-size: 0.6rem;
          color: $color-theme-d;
        }
        .text{
          flex: 1;
          font-size: $font-size-medium;
          color: $color-text-l;
        }
        .clear{
          .icon-clear{
            font-size: $font-size-medium ;
            color: $color-text-d;
            position: relative;
            top: -0.25rem;
          }
        }
      }
    }
    .list-content{
      max-height: 4.8rem;
      overflow: hidden;
      ul{
        .item{
          display: flex;
          align-items: center;
          height: 0.8rem;
          padding: 0 0.6rem 0 0.4rem;
          &.list-enter-active, &.list-leave-active{
            transition: all 0.1s
          }
          &.list-enter, &.list-leave-to{
            height: 0;
          }
          .current{
            width: 0.4rem;
            font-size: $font-size-medium;
            color: $color-theme-d;
          }
          .text{
            flex: 1;
            @include ellipse();
            font-size: $font-size-medium;
            color: $color-text-d;
          }
          .like{
            margin-right: 0.3rem;
            font-size: $font-size-medium;
            color: $color-theme;
            .icon-favorite{
              color: red;
            }
          }
          .delete{
            .icon-delete{
              font-size: 0.2rem;
              color: $color-theme;
              position: relative;
              top: -0.25rem;
            }
          }
        }
      }
    }
    .list-operate{
      width: 2.8rem;
      margin: 0.4rem auto 0.6rem auto;
      .add{
        display: flex;
        align-items: center;
        padding: 0.16rem 0.32rem;
        border: 1px solid $color-text-l;
        border-radius: 2rem;
        color: $color-text-l;
        .icon-add{
          margin-right: 0.1rem;
          font-size: $font-size-small-s;
        }
        .text{
          font-size: $font-size-small;
        }
      }
    }
    .list-close{
      width: 100%;
      text-align: center;
      line-height: 1rem;
      background: $color-background;
      font-size: $font-size-medium-x;
      color: $color-text-l;
    }
  }
}
</style>
